<?php

require_once realpath (dirname (__FILE__) . '//..//..//pages.php');
require_once CLASS_DATABASE;
require_once CLASS_FORM;

define ('LBL_MSG', 'Statistiche di:');

function formClasse () {
    /* Creating form */
    $form = new Form (
                    array (
                        'action' => HTTP_CTRL_MAIN,
                        'class' => 'details',
                        'id' => 'form',
                        'method' => 'get'
                    )
    );

    $form->add (new Input (array (
                'name' => 'stat',
                'type' => 'hidden',
                'value' => $stat
                    )
            )
    );

    $db = openConnection ();

    $result = $db->select ('classe', array ('ID', 'nome'), null, null, 'nome');
    $select = new Select (array (
                'name' => 'IDclasse'
                    )
    );
    while ($row = mysql_fetch_assoc ($result)) {
        $select->add (new Option ($row['nome'], $row['ID']));
    }

    $form->add (new Label (
                    LBL_MSG . 'classe',
                    $select
            )
    );

    $form->add (new Button (
                    array (
                        'type' => 'submit',
                    ),
                    'Mostra'
            )
    );
    $form->add (new Br ());
    $form->add (new Br ());

    return $form;
}

function formInterventoDocenti () {
    /* Creating form */
    $form = new Form (
                    array (
                        'action' => HTTP_CTRL_MAIN,
                        'class' => 'details',
                        'id' => 'form',
                        'method' => 'get'
                    )
    );

    $form->add (new Input (array (
                'name' => 'stat',
                'type' => 'hidden',
                'value' => 'intervento_docente'
                    )
            )
    );

    $db = openConnection ();

    /* Adding studente */
    $result = $db->select ('docente', array ('ID', 'nome', 'cognome'), null, null, 'cognome');
    $select = new Select (array (
                'name' => 'IDdocente'
                    )
    );
    while ($row = mysql_fetch_assoc ($result)) {
        $select->add (new Option ($row['cognome'] . ' ' . $row['nome'], $row['ID']));
    }
    $form->add (new Label (
                    'Interventi del docente ',
                    $select
            )
    );

    $form->add (new Button (
                    array (
                        'type' => 'submit',
                    ),
                    'Mostra'
            )
    );
    $form->add (new Br ());
    $form->add (new Br ());

    return $form;
}

function formInterventoStudenti () {
    /* Creating form */
    $form = new Form (
                    array (
                        'action' => HTTP_CTRL_MAIN,
                        'class' => 'details',
                        'id' => 'form',
                        'method' => 'get'
                    )
    );

    $form->add (new Input (array (
                'name' => 'stat',
                'type' => 'hidden',
                'value' => 'intervento_studente'
                    )
            )
    );

    $db = openConnection ();

    /* Adding studente */
    $result = $db->select ('studente', array ('ID', 'nome', 'cognome'), null, null, 'cognome');
    $select = new Select (array (
                'name' => 'IDstudente'
                    )
    );
    while ($row = mysql_fetch_assoc ($result)) {
        $select->add (new Option ($row['cognome'] . ' ' . $row['nome'], $row['ID']));
    }
    $form->add (new Label (
                    'Interventi dello studente ',
                    $select
            )
    );

    $form->add (new Button (
                    array (
                        'type' => 'submit',
                    ),
                    'Mostra'
            )
    );
    $form->add (new Br ());
    $form->add (new Br ());

    return $form;
}

function formMostra ($stat, $fields = null) {
    /* Creating form */
    $form = new Form (
                    array (
                        'action' => HTTP_CTRL_MAIN,
                        'class' => 'details',
                        'id' => 'form',
                        'method' => 'get'
                    )
    );
    $form->add (new Br ());
    if (is_string ($fields)) {
        $fields = array ($fields);
    }
    if (!array_search ('ID', $fields)) {
        $fields[] = 'ID';
    }

    $db = openConnection ();
    $result = $db->select ($stat, $fields);
    $n = $db->numberOfRows ($result);
    if ($n <= 3) {
        $n = 1;
    } else if ($n > 10) {
        $n = 10;
    }
    $select = new Select (array (
                'name' => 'ID',
                'size' => $n
                    )
    );
    while ($row = $db->fetchAssoc ($result)) {
        $label = '';
        foreach ($fields as $field) {
            if ($field != 'ID') {
                $label .= $row[$field] . ' ';
            }
        }
        $label = substr ($label, 0, strlen ($label) - 1);
        $select->add (new Option ($label, $row['ID']));
    }
    $form->add (new Label (
                    'Dettagli per ',
                    $select
            )
    );

    $form->add (new Input (array (
                'name' => 'stat',
                'type' => 'hidden',
                'value' => $stat
                    )
            )
    );

    $form->add (new Button (
                    array (
                        'type' => 'submit',
                    ),
                    'Mostra'
            )
    );
    $form->add (new Br ());
    $form->add (new Br ());

    return $form;
}

?>
